package cn.com.surker.workcon.service.zhcl;

import cn.com.surker.base.system.dao.SysStorageLogDao;
import cn.com.surker.base.system.entity.SysLine;
import cn.com.surker.base.system.entity.SysStorageLog;
import cn.com.surker.control.entity.ControlExpend;
import cn.com.surker.enums.StateEnum;
import cn.com.surker.lab.dao.LabExpendBatchDao;
import cn.com.surker.lab.entity.LabExpend;
import cn.com.surker.lab.entity.LabExpendBatch;
import cn.com.surker.lab.entity.LabExpendOther;
import cn.com.surker.lab.vo.LabExpendVoB;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.StringUtils;
import cn.com.surker.workcon.common.GKJDBCService;
import cn.com.surker.workcon.service.DosageService;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 珠海长路工消耗信息 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2024-09-26
 */
public class DosageServiceImpl extends GKJDBCService implements DosageService {

	@Resource
	private SysStorageLogDao sysStorageLogDao;

	/**
	 * 盘数据
	 */
	@Resource
	private LabExpendBatchDao labExpendBatchDao;

	/**
	 * 根据盘id,查询工控机消耗信息
	 *
	 * @param pieId
	 * @param batchStere
	 * @return
	 */
	public List<LabExpend> findByPieId(Long lineId, Long invoiceId, Long batchId, String prodId, String pieId, Date batTime,
									   Double batchStere, Long formulaId, Double formulaWater, Long levelOptimizeId, Date createDate) {
		if (lineId == null || StringUtils.isBlank(pieId)) {
			return null;
		}
		SysLine line = getLine(lineId);
		if (line == null) {
			return null;
		}
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		//	查询sql
		String panTableName = ZhclUtil.getRecordTableName(line, "N", "P");
		String querySql = "select BNo,Btch,StNo,T0,F0,A0,T1,F1,A1,T2,F2,A2,T3,F3,A3,T4,F4,A4," +
			"T5,F5,A5,T6,F6,A6,T7,F7,A7,T8,F8,A8,T9,F9,A9,T10,F10,A10,T11,F11,A11,T12,F12,A12," +
			"T13,F13,A13,T14,F14,A14,T15,F15,A15,T16,F16,A16,T17,F17,A17,Flag3,SpI,MOTm,Rflag,SysGuid " +
			"FROM " + panTableName + " where BNo = ? and SysGuid = ?;";
		Object[] param = {prodId, pieId};
		//	查询消耗
		List<Map<String, Object>> dosageList = lineJdbcTemp.queryForList(querySql, param);
		if (dosageList == null || dosageList.isEmpty()) {
			return null;
		}
		//	转换数据
		Map<String, Object> dosage = dosageList.get(0);
		String rq = String.valueOf(dosage.get("SpI"));
		String motm = String.valueOf(dosage.get("MOTm"));
		//	时间
		String dateStr = rq + " " + motm;
		Date datTim = DateUtil.convertDate(dateStr, "yyyyMMdd HH:mm:ss");
		// 转换消耗格式
		String useTime = DateUtil.convertString(datTim);
		//	仓位表
		String storeHouseTable = ZhclUtil.getCtrlTableName(line, "StoreHouse");
		//	材料表
		String materialTable = ZhclUtil.getCtrlTableName(line, "Material");
		//	查询仓位数据
		String queryWCStoreSql = "SELECT a.Name storeName,b.code materialCode,b.name materialName\n" +
			"  FROM " + storeHouseTable + " a\n" +
			"  left join " + materialTable + " b on a.loadMaterial = b.code";
		//	工控机的材料
		List<Map<String, Object>> storeList = lineJdbcTemp.queryForList(queryWCStoreSql);
		//	匹配消耗
		List<LabExpend> expendList = new ArrayList<>();
		for (int i = 0, s = storeList.size(); i < s; i++) {
			//	当个仓位
			Map<String, Object> storeMap = storeList.get(i);
			if (storeMap == null) {
				continue;
			}
			//	材料编码
			Object materialCode = storeMap.get("materialCode");
			//	工控机的料仓信息,根据料仓信息查询出当前消耗的材料
			String storage = String.valueOf(storeMap.get("storeName"));
			//	工控机的原料
			String bcsMaterial = String.valueOf(storeMap.get("materialName"));
			//	根据材料编码查询数据材料的消耗
			////	1.拼接数据表字段
			//	工控机的配方值
			String recAmntColumn = "T" + materialCode;
			//	工控机的实际值
			String factAmntColumn = "F" + materialCode;
			// 取出值
			Double recAmnt = Double.valueOf(String.valueOf(dosage.get(recAmntColumn)));
			Double factAmnt = Double.valueOf(String.valueOf(dosage.get(factAmntColumn)));
			if ((recAmnt == null || recAmnt == 0d) && factAmnt == null || factAmnt == 0d) {
				continue;
			}
			//	工控机的含水率
			Double watFull = 1.0;
			///
			//	erp的原料id
			Long erpMaterialId = null;
			//	工控机的原料id
			Long erpStorageId = null;
			SysStorageLog takeTimeOrStorage = sysStorageLogDao.findTakeTimeOrStorage(lineId, storage, 1, useTime);
			if (takeTimeOrStorage != null) {
				String erpMaterial = takeTimeOrStorage.getMaterial();
				erpMaterialId = takeTimeOrStorage.getMaterialId();
				erpStorageId = takeTimeOrStorage.getStorageId();
			}
			LabExpend expend = new LabExpend();
			expend.setInvoiceId(invoiceId);
			expend.setLineId(lineId);
			expend.setMaterialId(erpMaterialId);
			expend.setStorageId(erpStorageId);
			expend.setControlStorage(storage);
			expend.setControlMaterial(bcsMaterial);
			expend.setBatchId(batchId);
			expend.setFormulaId(formulaId);
			expend.setFormulaQuantity(recAmnt);
			expend.setFormulaWater(formulaWater);
			expend.setControlSetQuantity(recAmnt);
			expend.setControlRealQuantity(factAmnt);
			expend.setControlWater(watFull);
			expend.setCreator(bcsCreater);
			expend.setCreateTime(createDate);
			expend.setUseTime(batTime);
			//	存入序号
			expend.setConExpendId(materialCode.toString());
			expendList.add(expend);
		}
		return expendList;
	}

	@Override
	public List<ControlExpend> findWorkByPieId(Long lineId, Long batchId, String prodId, String pieId, Date batTime, Date createDate) {
		if (lineId == null || StringUtils.isBlank(pieId)) {
			return null;
		}
		SysLine line = getLine(lineId);
		if (line == null) {
			return null;
		}
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		//	查询sql
		String panTableName = ZhclUtil.getRecordTableName(line, "N", "P");
		String querySql = "select BNo,Btch,StNo,T0,F0,A0,T1,F1,A1,T2,F2,A2,T3,F3,A3,T4,F4,A4," +
			"T5,F5,A5,T6,F6,A6,T7,F7,A7,T8,F8,A8,T9,F9,A9,T10,F10,A10,T11,F11,A11,T12,F12,A12," +
			"T13,F13,A13,T14,F14,A14,T15,F15,A15,T16,F16,A16,T17,F17,A17,Flag3,SpI,MOTm,Rflag,SysGuid " +
			"FROM " + panTableName + " where BNo = ? and SysGuid = ?;";
		Object[] param = {prodId, pieId};
		//	查询消耗
		List<Map<String, Object>> dosageList = lineJdbcTemp.queryForList(querySql, param);
		if (dosageList == null || dosageList.isEmpty()) {
			return null;
		}
		//	转换数据
		Map<String, Object> dosage = dosageList.get(0);
		//	日期
		String rq = String.valueOf(dosage.get("SpI"));
		//	时间
		String motm = String.valueOf(dosage.get("MOTm"));
		//	时间
		String dateStr = rq + " " + motm;
		Date datTim = DateUtil.convertDate(dateStr, "yyyyMMdd HH:mm:ss");
		// 转换消耗格式
		String useTime = DateUtil.convertString(datTim);
		//	仓位表
		String storeHouseTable = ZhclUtil.getCtrlTableName(line, "StoreHouse");
		//	材料表
		String materialTable = ZhclUtil.getCtrlTableName(line, "Material");
		//	查询仓位数据
		String queryWCStoreSql = "SELECT a.Name storeName,b.code materialCode,b.name materialName\n" +
			"  FROM " + storeHouseTable + " a\n" +
			"  left join " + materialTable + " b on a.loadMaterial = b.code";
		//	工控机的材料
		List<Map<String, Object>> storeList = lineJdbcTemp.queryForList(queryWCStoreSql);
		//	存储消耗
		List<ControlExpend> expendList = new ArrayList<>();
		for (int i = 0, s = storeList.size(); i < s; i++) {
			//	当个仓位
			Map<String, Object> storeMap = storeList.get(i);
			if (storeMap == null) {
				continue;
			}
			//	材料编码
			Object materialCode = storeMap.get("materialCode");
			//	工控机的料仓信息,根据料仓信息查询出当前消耗的材料
			String storage = String.valueOf(storeMap.get("storeName"));
			//	工控机的原料
			String bcsMaterial = String.valueOf(storeMap.get("materialName"));
			//	根据材料编码查询数据材料的消耗
			////	1.拼接数据表字段
			//	工控机的配方值
			String recAmntColumn = "T" + materialCode;
			//	工控机的实际值
			String factAmntColumn = "F" + materialCode;
			// 取出值
			Double recAmnt = Double.valueOf(String.valueOf(dosage.get(recAmntColumn)));
			Double factAmnt = Double.valueOf(String.valueOf(dosage.get(factAmntColumn)));
			if ((recAmnt == null || recAmnt == 0d) && factAmnt == null || factAmnt == 0d) {
				continue;
			}
			//	工控机的含水率
			Double watFull = 1.0;
			//	erp的原料id
			Long erpMaterialId = null;
			//	工控机的原料id
			Long erpStorageId = null;
			SysStorageLog takeTimeOrStorage = sysStorageLogDao.findTakeTimeOrStorage(lineId, storage, 1, useTime);
			if (takeTimeOrStorage != null) {
				String erpMaterial = takeTimeOrStorage.getMaterial();
				erpMaterialId = takeTimeOrStorage.getMaterialId();
				erpStorageId = takeTimeOrStorage.getStorageId();
			}
			ControlExpend expend = new ControlExpend();
			expend.setLineId(lineId);
			expend.setErpBatchId(batchId);
			expend.setStorage(storage);
			expend.setMaterial(bcsMaterial);
			expend.setErpMaterialId(erpMaterialId);
			expend.setErpStorageId(erpStorageId);
			expend.setFormulaQuantity(recAmnt);
			expend.setControlSetQuantity(recAmnt);
			expend.setControlRealQuantity(factAmnt);
			expend.setControlWater(watFull);
			expend.setUseTime(batTime);
			expend.setStatus(StateEnum.ENABLED.getCode());
			expend.setCreator(bcsCreater);
			expend.setCreateTime(createDate);
			expend.setConExpendNo(materialCode.toString());
			expendList.add(expend);
		}

		return expendList;
	}

	@Override
	public List<LabExpendOther> findAll(Long lineId) {
		return null;
	}

	@Override
	public LabExpend queryByDosId(Long lineId, Long batchId, String dosId) {
		if (lineId == null || StringUtils.isBlank(dosId)) {
			return null;
		}
		SysLine line = getLine(lineId);
		if (line == null) {
			return null;
		}
		JdbcTemplate lineJdbcTemp = getLineJdbcTemp(line);
		if (lineJdbcTemp == null) {
			return null;
		}
		//	查询盘数据
		LabExpendBatch labExpendBatch = labExpendBatchDao.selectById(batchId);
		if (labExpendBatch == null) {
			return null;
		}
		//	获取工控盘id
		String conBatchId = labExpendBatch.getConBatchId();
		String panTableName = ZhclUtil.getRecordTableName(line, "N", "P");
		String querySql = "select top 1 BNo,Btch,StNo,T0,F0,A0,T1,F1,A1,T2,F2,A2,T3,F3,A3,T4,F4,A4," +
			"T5,F5,A5,T6,F6,A6,T7,F7,A7,T8,F8,A8,T9,F9,A9,T10,F10,A10,T11,F11,A11,T12,F12,A12," +
			"T13,F13,A13,T14,F14,A14,T15,F15,A15,T16,F16,A16,T17,F17,A17,Flag3,SpI,MOTm,Rflag,SysGuid " +
			"FROM " + panTableName + " where SysGuid = ?;";
		Object[] param = {conBatchId};

		List<Map<String, Object>> dosageList = lineJdbcTemp.queryForList(querySql, param);
		if (dosageList == null || dosageList.isEmpty()) {
			return null;
		}
		Map<String, Object> dosage = dosageList.get(0);
		if (dosage == null) {
			return null;
		}
		//	仓位表
		String storeHouseTable = ZhclUtil.getCtrlTableName(line, "StoreHouse");
		//	材料表
		String materialTable = ZhclUtil.getCtrlTableName(line, "Material");
		//	查询仓位数据
		String queryWCStoreSql = "SELECT a.Name storeName,b.code materialCode,b.name materialName\n" +
			"  FROM " + storeHouseTable + " a\n" +
			"  left join " + materialTable + " b on a.loadMaterial = b.code where b.code = " + dosId;
		//	工控机的材料
		List<Map<String, Object>> storeList = lineJdbcTemp.queryForList(queryWCStoreSql);
		if (storeList == null || storeList.isEmpty()) {
			return null;
		}
		Map<String, Object> store = storeList.get(0);
		Object storeName = store.get("storeName");
		Object materialName = store.get("materialName");
		//	工控机的配方值
		String recAmntColumn = "T" + dosId;
		//	工控机的实际值
		String factAmntColumn = "F" + dosId;
		Double recAmnt = Double.valueOf(String.valueOf(dosage.get(recAmntColumn)));
		Double factAmnt = Double.valueOf(String.valueOf(dosage.get(factAmntColumn)));
		//	含水率
		Double formulaWater = 1.0;
		//	日期
		String rq = String.valueOf(dosage.get("SpI"));
		//	时间
		String motm = String.valueOf(dosage.get("MOTm"));
		//	时间
		String dateStr = rq + " " + motm;
		Date datTim = DateUtil.convertDate(dateStr, "yyyyMMdd HH:mm:ss");
		LabExpend expend = new LabExpend();
		expend.setLineId(lineId);
		//	工控仓位
		expend.setControlStorage(String.valueOf(storeName));
		expend.setControlMaterial(String.valueOf(materialName));
		expend.setBatchId(batchId);
		expend.setFormulaQuantity(recAmnt);
		expend.setFormulaWater(formulaWater);
		expend.setControlSetQuantity(recAmnt);
		expend.setControlRealQuantity(factAmnt);
		expend.setControlWater(formulaWater);
		expend.setCreator(bcsCreater);
		expend.setCreateTime(new Date());
		expend.setUseTime(datTim);
		//	存入序号
		expend.setConExpendId(dosId);
		return expend;
	}

	@Override
	public List<Long> writeBackB(Long lineId, List<LabExpendVoB> labExpends) {
		return null;
	}

}
